/**
 * web-upload 工具类
 * 
 * 约定：
 * 上传按钮的id = 图片隐藏域id + 'BtnId'
 * 图片预览框的id = 图片隐藏域id + 'PreId'
 * 
 * @author cyf
 */
(function() {
	
	var $WebUpload = function(pictureId) {
		this.pictureId = pictureId;
		this.uploader = null;
		this.uploadBtnId = pictureId + "BtnId";
		this.uploadPreId = pictureId + "PreId";
		this.uploadUrl = Sys.ctxPath + '/file/upload';
		this.fileSizeLimit = 100 * 1024 * 1024;
		this.picWidth = 800;
		this.picHeight = 800;
        this.accept = null;
		this.uploadBarId = pictureId+"_progressBar";
		this.uploadCompleteEvent=null;
		this.response = null; //上传后返回回来的参数 字符串格式
	};

	$WebUpload.prototype = {
		/**
		 * 初始化webUploader
		 */
		init : function() {
			this.uploader = this.create();
			this.bindEvent();
			return this.uploader;
		},
		
		/**
		 * 创建webuploader对象
		 */
		create : function() {
			var webUploader = WebUploader.create({
				auto : true,
				pick : {
					id : '#' + this.uploadBtnId,
					multiple : false,// 只上传一个
				},
				accept :this.accept,
				swf : Sys.ctxPath
					+ '/static/js/plugins/webuploader/Uploader.swf',
				disableGlobalDnd : true,
				duplicate : true,
				server : this.uploadUrl,
				fileSingleSizeLimit : this.fileSizeLimit
			});
			
			return webUploader;
		},

		/**
		 * 绑定事件
		 */
		bindEvent : function() {
			var me =  this;
			me.uploader.on('fileQueued', function(file) {
				var _preId = $("#" + me.uploadPreId);
				if(_preId){
					var $li = $('<div><img width="100px" height="100px"></div>');
					var $img = $li.find('img');
	
					$("#" + me.uploadPreId).html($li);
	
					// 生成缩略图
					me.uploader.makeThumb(file, function(error, src) {
						if (error) {
							$img.replaceWith('<span>不能预览</span>');
							return;
						}
						$img.attr('src', src);
					}, me.picWidth, me.picHeight);
				}
			});

			// 某个文件开始上传前触发，一个文件只会触发一次。
			me.uploader.on('uploadStart', function(file, percentage) {
				$("#"+me.uploadBarId).show();
			});
			
			// 文件上传过程中创建进度条实时显示。
			me.uploader.on('uploadProgress', function(file, percentage) {
				$("#"+me.uploadBarId).find("div").css("width",percentage * 100 + "%");
				
			});

			// 文件上传成功，给item添加成功class, 用样式标记上传成功。
			me.uploader.on('uploadSuccess', function(file,response) {
				setTimeout(function(){
					$("#"+me.uploadBarId).hide();
					$("#"+me.uploadBarId).find("div").css("width","0");
				},500)
				
				Sys.success("上传成功");
				me.response = response;
				var _id = $("#" + me.pictureId);
				if(_id){
					_id.val(response);
				}
			});

			// 文件上传失败，显示上传出错。
			me.uploader.on('uploadError', function(file) {
				setTimeout(function(){
					$("#"+me.uploadBarId).hide();
					$("#"+me.uploadBarId).find("div").css("width","0");
				},500)
				Sys.error("上传失败");
			});

			// 其他错误
			me.uploader.on('error', function(type) {
				if ("Q_EXCEED_SIZE_LIMIT" == type) {
					Sys.error("文件大小超出了限制");
				} else if ("Q_TYPE_DENIED" == type) {
					Sys.error("文件类型不满足");
				} else if ("Q_EXCEED_NUM_LIMIT" == type) {
					Sys.error("上传数量超过限制");
				} else if ("F_DUPLICATE" == type) {
					Sys.error("图片选择重复");
				} else {
					Sys.error("上传过程中出错");
				}
			});

			// 完成上传完了，成功或者失败
			me.uploader.on('uploadComplete', function(file) {
				if(me.uploadCompleteEvent){
                    me.uploadCompleteEvent(me.response);
                }
			});
		},
		/**
		 * 设置图片上传的进度条的id
		 */
		setUploadBarId: function (id) {
			this.uploadBarId = id;
		},
		setUploadUrl: function (url) {
			this.uploadUrl = url;
		},
		setData: function (data) {
			this.uploader.options.formData = data;
		},
        setUploadCompleteEvent: function (func) {
            this.uploadCompleteEvent = func;
        },
        //设置上传文件类型
        setUploadAccept: function (accept) {
            this.accept = accept;
        }
	};

	window.$WebUpload = $WebUpload;

}());